<template>
    <!-- 新增文章弹框 -->
  <el-dialog
    :title="articleForm.id ? '编辑文章' : '新增文章'"
    :visible="newVisible"
    width="60%"
    @close="newArtDialogClose">
    <el-form :model="articleForm" :rules="articleRules" ref="articleRef" label-width="100px">
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="articleForm.title" placeholder="请输入文章标签"></el-input>
      </el-form-item>
      <el-form-item label="文章内容" prop="articleBody">
          <quill-editor 
            v-model="articleForm.articleBody"
            @blur="$refs.articleRef.validateField('content')"
            style="height: 200px; display: inline-block"
          ></quill-editor>
      </el-form-item>
      <el-form-item label="视频地址">
        <el-input v-model="articleForm.videoURL" placeholder="请输入视频地址"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" style="text-align: right">
      <el-button @click="newArtDialogClose">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
// 富文本编辑器
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'

import { add, update,detail } from  '@/api/hmmm/articles'

export default {
  props: {
    newVisible: {
      type: Boolean,
      require: true
    },
    params: {
      type: Object,
      require: true
    },
    editFrom: {
      type: Object,
      require: true
    }
  },
  components: {
    quillEditor
  },
  data() {
    return {
      // 新增文章表单数据
      articleForm: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      // 新增文章表单验证规则
      articleRules: {
        title: [
          { required: true, message: '请填写文章标题', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请填写文章内容', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    newArtDialogClose() {
      this.$emit('newArt-dialog-close')
      this.$refs.articleRef.resetFields()
      // 清空表单
      this.articleForm = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
    },
    // 提交表单
    submit() {
      // 兜底校验
      this.$refs.articleRef.validate(async valid => {
        if(!valid) return
        if(this.articleForm.id) {
          // 编辑
          const res = await update(this.articleForm)
        } else {
          // 添加
          const res = await add(this.articleForm)
        }

        this.$message.success(this.articleForm.id ?  '编辑文章成功' : '添加文章成功')
        // 返回第一页
        this.params.page = 1
        // 重新渲染
        this.$emit('fetch-articles-list')
        // 关闭弹窗
        this.newArtDialogClose()
      })
    },
    // 获取文章详细信息
    async fetchDetailArticle(id) {
      const {data} = await detail({id})
      // console.log('data', data);
      this.articleForm = data
      // this.getUsername()
      // this.$emit('showPreviewDialog', data)
    },
    // 获取 预览当前点击的文章作者
    getUsername() {
      this.articlesList.forEach(item => {
        if(item.id === this.articleId){
          this.articleForm = item.username
          // console.log(this.article.username);
        }
      })
    },
  }
}
</script>

<style scoped lang='less'></style>
